package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;

import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfServiceAreaBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.NumberConstant;
import com.sgb.kjwl.view.ui.adapter.self.packers.FlowLayoutAdapter2;
import com.sgb.kjwl.view.widget.MyItemDecoration;
import com.sgb.kjwl.viewmodel.self.packers.SelfServiceAreaModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.FlowLayoutManager;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rv.daimhim.rvdecoration.DecorationBuilder;

/**
 *
 * @author WYJ
 * @date 2020/8/27
 */
public class SelfServiceAreaActivity extends AppActivity {

    @Inject
    SelfServiceAreaModel mModel;
    private ActivitySelfServiceAreaBinding mBind;

    private FlowLayoutAdapter2 mAdapter2;
    private List<AddressEntity> list;

    public static void start(AppActivity context, List<SelfTeamIntroduceEntity.ServiceAreasBean> data, int code, String title) {
        Intent intent = new Intent(context, SelfServiceAreaActivity.class);
        intent.putExtra(AppConstant.EXTRA_ENTITY, (Serializable) data);
        intent.putExtra("title", title);
        context.startActivityForResult(intent, code);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setTitle(getIntent().getStringExtra("title"));
    }

    @Override
    protected void initView() {
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_self_service_area);
        mBind.setViewModel(null);
        mBind.tvTitle.setText(getIntent().getStringExtra("title"));

        List<SelfTeamIntroduceEntity.ServiceAreasBean> data = (List<SelfTeamIntroduceEntity.ServiceAreasBean>) getIntent().getSerializableExtra(AppConstant.EXTRA_ENTITY);
        mBind.recycler.setLayoutManager(new FlowLayoutManager());
        mBind.recycler.addItemDecoration(new MyItemDecoration(this));
        mAdapter2 = new FlowLayoutAdapter2(this, data);
        mAdapter2.setDeleteImpl(() -> {
            List<AddressEntity> list = new ArrayList<>();
            for (SelfTeamIntroduceEntity.ServiceAreasBean data1 : mAdapter2.getDatas()) {
                list.add(new AddressEntity(data1.getAreaNo(), data1.getName(), data1.getAreaNo()));
            }
            mModel.mPopWindow.setCitySelectItem(list);
        });
        mBind.recycler.setAdapter(mAdapter2);

        mBind.btnAdd.setOnClickListener(this);
        mBind.tvFinish.setOnClickListener(this);
        mBind.tvDeleteAll.setOnClickListener(this);
        mBind.ivDelete.setOnClickListener(this);
        mBind.btnRelease.setOnClickListener(this);

        if (data != null && data.size() > 0){
            list = new ArrayList<>();
            for (SelfTeamIntroduceEntity.ServiceAreasBean bean : data) {
                list.add(new AddressEntity(bean.getAreaNo(),bean.getName(),bean.getAreaNo()));
            }
        }

        mModel.getCityData();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_add) {
            mModel.showPop(v);
        } else if (id == R.id.tv_delete_all) {
            mAdapter2.getDatas().clear();
            mAdapter2.notifyDataSetChanged();
            List<AddressEntity> list = new ArrayList<>();
            for (SelfTeamIntroduceEntity.ServiceAreasBean data : mAdapter2.getDatas()) {
                list.add(new AddressEntity(data.getAreaNo(),data.getName(),data.getAreaNo()));
            }
            mModel.mPopWindow.setCitySelectItem(list);
        } else if (id == R.id.iv_delete) {
            mAdapter2.setDelete(true);
            mBind.deleteLayout.setVisibility(View.VISIBLE);
            mBind.ivDelete.setVisibility(View.GONE);
            List<AddressEntity> list = new ArrayList<>();
            for (SelfTeamIntroduceEntity.ServiceAreasBean data : mAdapter2.getDatas()) {
                list.add(new AddressEntity(data.getAreaNo(),data.getName(),data.getAreaNo()));
            }
            mModel.mPopWindow.setCitySelectItem(list);
        } else if (id == R.id.tv_finish) {
            mAdapter2.setDelete(false);
            mBind.deleteLayout.setVisibility(View.GONE);
            mBind.ivDelete.setVisibility(View.VISIBLE);
            List<AddressEntity> list = new ArrayList<>();
            for (SelfTeamIntroduceEntity.ServiceAreasBean data : mAdapter2.getDatas()) {
                list.add(new AddressEntity(data.getAreaNo(),data.getName(),data.getAreaNo()));
            }
            mModel.mPopWindow.setCitySelectItem(list);
        } else if (id == R.id.btn_release) {
            if (mAdapter2.getDatas().size() == 0) {
                MToast.showToast(this, "至少选择一个" + getIntent().getStringExtra("title") + "地址");
                return;
            }
            if (mAdapter2.getDatas().size() > NumberConstant.NUMBER_TWENTY) {
                MToast.showToast(this, getIntent().getStringExtra("title") + "最多显示20条");
                return;
            }
            setResult(RESULT_OK, new Intent().putExtra(AppConstant.EXTRA_OBJ, (Serializable) mAdapter2.getDatas()));
            finish();
        }
    }

    public void updateAddress(List<SelfTeamIntroduceEntity.ServiceAreasBean> opt, SelfTeamIntroduceEntity.ServiceAreasBean bean) {
        mAdapter2.clearDatas();
        if (mAdapter2.getDatas().size() == 0) {
            if (opt != null) {
                mAdapter2.getDatas().addAll(opt);
            } else {
                if (bean != null){
                    mAdapter2.getDatas().add(bean);
                }
            }
        } else {
            List<SelfTeamIntroduceEntity.ServiceAreasBean> tempList = new ArrayList<>();
            if (opt != null) {
                for (int i = 0; i < opt.size(); i++) {
                    boolean contains = mAdapter2.getDatas().contains(opt.get(i));
                    if (!contains) {
                        tempList.add(opt.get(i));
                    }
                }
            } else {
                boolean contains = mAdapter2.getDatas().contains(bean);
                if (!contains) {
                    tempList.add(bean);
                }
            }
            mAdapter2.getDatas().addAll(tempList);
        }
        mAdapter2.notifyDataSetChanged();
    }

    public void initCity() {
        mModel.mPopWindow.setCitySelectItem(list);
    }

    @Override
    public void onBackPressed() {
        if(!mModel.mPopWindow.isShowing()){
            super.onBackPressed();
        }
    }
}
